// JavaScript Document
var registeredLocations = new Array();
var visitedLocations = new Array();
var map;
var MARKER = 0;
var CIRCLE_RADIUS = 1;
var LABEL = 2;
var MARKER_NAME = 3;

function initializeLocator(){
	var latlng = new google.maps.LatLng(20.97490,-89.61977);
	var myOptions = {
		zoom : 10,
		center : latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
}

function addVisitedLocationToList(lat,lng,position){
	var latlng = new google.maps.LatLng(lat,lng);
	var marker = new google.maps.Marker({position:latlng,map:null});
	visitedLocations[position] = marker;
}

function showVisitedLocation(position){
	var visitedLoc = visitedLocations[position];
	visitedLoc.setMap(map);
}

function hideVisitedLocation(position){
	var visitedLoc = visitedLocations[position];
	visitedLoc.setMap(null);
}

function addLocationToList(lat,lng,inRadius,name,position){
	var latlng = new google.maps.LatLng(lat,lng);
	var circleRadius = new google.maps.Circle({center:latlng,radius:inRadius,map:map});
	var actualMarker = new google.maps.Marker({position:latlng,map:map});
	var markerLabel = new Label({map:map});
	markerLabel.bindTo('position',actualMarker,'position');
	markerLabel.set('zIndex',1234);
	markerLabel.set('text',name);
	var dataStructure = new Array();
	dataStructure[MARKER] = actualMarker;
	dataStructure[CIRCLE_RADIUS] = circleRadius;
	dataStructure[LABEL] = markerLabel;
	dataStructure[MARKER_NAME] = name;
	registeredLocations[position] = dataStructure;
}

function getLocationName(lat,lng){
	var position = isLocationContained(lat,lng);
	var locationName = new String();
	if(position != -1){
		var location = registeredLocations[position];
		locationName = location[MARKER_NAME];
	}else{
		locationName = "Unknown Location";
	}
	return locationName;
}

function isLocationContained(lat,lng){
	var latlng = new google.maps.LatLng(lat,lng);
	for(var i=0;i<registeredLocations.length;i += 1){
		var location = registeredLocations[i];
		var locationRadius = location[CIRCLE_RADIUS];
		var latBounds = locationRadius.getBounds();
		if(latBounds.contains(latlng)){
			return i;
		}
	}
	return -1;
}

function hideAllRegisteredLocations(){
	for(var i=0;i<registeredLocations.length;i += 1){
		var location = registeredLocations[i];
		var marker = location[MARKER];
		marker.setMap(null);
		var label = location[LABEL];
		label.setMap(null);
		var circle = location[CIRCLE_RADIUS];
		circle.setMap(null);
	}
}

function showAllRegisteredLocations(){
	for(var i=0;i<registeredLocations.length;i += 1){
		var location = registeredLocations[i];
		var marker = location[MARKER];
		marker.setMap(map);
		var label = location[LABEL];
		label.setMap(map);
		var circle = location[CIRCLE_RADIUS];
		circle.setMap(map);
	}
}